package com.mf.framework.common.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.mf.framework.common.exception.ResultException;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;

/**
 * 通用结果集实体
 * User: zhaoming
 * Date: 17-5-9
 * Time: 下午4:14
 * To change this template use File | Settings | File Templates.
 */
public class Result<T> implements Serializable {

	private static final long serialVersionUID = -1;
	/**
	 * 状态编码
	 */
	private Integer statusCode;
	/**
	 * 错误消息
	 */
	private String errorCode;
	/**
	 * 错误消息
	 */
	private String errorMessage;
	/**
	 * 返回值对象
	 */
	private T body;

	public Result() {
		this.statusCode = HttpServletResponse.SC_OK;
	}

	public Result(T body) {
		this.statusCode = HttpServletResponse.SC_OK;
		this.body = body;
	}

	public Result(Integer statusCode, String errorMessage){
		this.statusCode = statusCode;
		this.errorMessage = errorMessage;
	}

	public Result(Integer statusCode, String errorCode, String errorMessage){
		this.statusCode = statusCode;
		this.errorCode = errorCode;
		this.errorMessage = errorMessage;
	}

	public Integer getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(Integer statusCode) {
		this.statusCode = statusCode;
	}

	public String getErrorCode() {
		return errorCode;
	}

	public void setErrorCode(String errorCode) {
		this.errorCode = errorCode;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public T getBody() {
		return body;
	}

	public void setBody(T body) {
		this.body = body;
	}

	@JsonIgnore
	public T getContent() {
		if(this.getStatusCode() == HttpServletResponse.SC_OK){
			return body;
		}else{
			throw new ResultException(this.getStatusCode(), this.getErrorCode(), this.getErrorMessage());
		}
	}
}
